﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Android_Helper.android
{
    class AndroidSwipeRefreshLayoutAndCode
    {
        public static string GetLayoutString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<android.support.v4.widget.SwipeRefreshLayout");
            sb.AppendLine("    android:id=\"@+id/srl_Refresh\"");
            sb.AppendLine("    android:layout_width=\"match_parent\"");
            sb.AppendLine("    android:layout_height=\"0dp\"");
            sb.AppendLine("    android:layout_weight=\"1\">");
            sb.AppendLine("");
            sb.AppendLine("    <LinearLayout");
            sb.AppendLine("        android:layout_width=\"match_parent\"");
            sb.AppendLine("        android:layout_height=\"match_parent\"");
            sb.AppendLine("        android:orientation=\"vertical\">");
            sb.AppendLine("");
            sb.AppendLine("        <LinearLayout");
            sb.AppendLine("            android:id=\"@+id/ll_data\"");
            sb.AppendLine("            android:layout_width=\"match_parent\"");
            sb.AppendLine("            android:layout_height=\"match_parent\"");
            sb.AppendLine("            android:gravity=\"center\"");
            sb.AppendLine("            android:orientation=\"vertical\"");
            sb.AppendLine("            android:visibility=\"gone\"");
            sb.AppendLine("            android:background=\"@color/bg_gray_ee\"");
            sb.AppendLine("            >");
            sb.AppendLine("");
            sb.AppendLine("            <android.support.v7.widget.RecyclerView");
            sb.AppendLine("                android:id=\"@+id/rv_list\"");
            sb.AppendLine("                android:layout_width=\"match_parent\"");
            sb.AppendLine("                android:layout_height=\"match_parent\"");
            sb.AppendLine("                >");
            sb.AppendLine("");
            sb.AppendLine("            </android.support.v7.widget.RecyclerView>");
            sb.AppendLine("        </LinearLayout>");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("        <LinearLayout");
            sb.AppendLine("            android:id=\"@+id/ll_nodata\"");
            sb.AppendLine("            android:layout_width=\"match_parent\"");
            sb.AppendLine("            android:layout_height=\"match_parent\"");
            sb.AppendLine("            android:gravity=\"center\"");
            sb.AppendLine("            android:orientation=\"vertical\">");
            sb.AppendLine("");
            sb.AppendLine("            <ImageView");
            sb.AppendLine("                android:layout_width=\"180dp\"");
            sb.AppendLine("                android:layout_height=\"180dp\"");
            sb.AppendLine("                android:src=\"@drawable/ico_dataempty\" />");
            sb.AppendLine("");
            sb.AppendLine("            <TextView");
            sb.AppendLine("                android:layout_width=\"wrap_content\"");
            sb.AppendLine("                android:layout_height=\"wrap_content\"");
            sb.AppendLine("                android:text=\"无数据\"");
            sb.AppendLine("                android:textColor=\"@color/font_blank_normal\"");
            sb.AppendLine("                android:textSize=\"@dimen/fw_h1\" />");
            sb.AppendLine("        </LinearLayout>");
            sb.AppendLine("");
            sb.AppendLine("    </LinearLayout>");
            sb.AppendLine("</android.support.v4.widget.SwipeRefreshLayout>");
            sb.AppendLine("");
            return sb.ToString();

        }

        public static string GetCodeString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("protected SwipeRefreshLayout srlRefresh;");
            sb.AppendLine("protected RecyclerView rvlist;");
            sb.AppendLine("protected LinearLayout llnodata;");
            sb.AppendLine("protected LinearLayout lldata;");
            sb.AppendLine("");
            sb.AppendLine("YJFRefreshListUtils refreshListUtils;");
            sb.AppendLine("List<Object> list;");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("@Override");
            sb.AppendLine("    protected void initView(View rootView) {");
            sb.AppendLine("        srlRefresh = rootView.findViewById(R.id.srl_Refresh);");
            sb.AppendLine("        rvlist = rootView.findViewById(R.id.rv_list);");
            sb.AppendLine("        llnodata = rootView.findViewById(R.id.ll_nodata);");
            sb.AppendLine("        lldata = rootView.findViewById(R.id.ll_data);");
            sb.AppendLine("");
            sb.AppendLine("        rvlist.setLayoutManager(new LinearLayoutManager(getActivity()));");
            sb.AppendLine("        rvlist.setItemAnimator(new DefaultItemAnimator());");
            sb.AppendLine("        rvlist.setHasFixedSize(true);");
            sb.AppendLine("        rvlist.setAdapter(new ListRecyclerViewAdapter());");
            sb.AppendLine("");
            sb.AppendLine("        list = new ArrayList<>();");
            sb.AppendLine("        refreshListUtils = new YJFRefreshListUtils(srlRefresh, rvlist, 10, 0, list);");
            sb.AppendLine("        refreshListUtils.setPanelData(lldata);");
            sb.AppendLine("        refreshListUtils.setPanelNoData(llnodata);");
            sb.AppendLine("        refreshListUtils.setLoadData(new ListRecyclerViewLoadData());");
            sb.AppendLine("    }");
            sb.AppendLine("");
            sb.AppendLine("@Override");
            sb.AppendLine("protected void fillData(View rootView) {");
            sb.AppendLine("    refreshListUtils.reloadData();");
            sb.AppendLine("}");
            sb.AppendLine("");
            sb.AppendLine("private class ListRecyclerViewLoadData implements YJFRefreshListUtils.LoadData {");
            sb.AppendLine("");
            sb.AppendLine("        @Override");
            sb.AppendLine("        public void load(int pageSize, int pageNumber) {");
            sb.AppendLine("            srlRefresh.setRefreshing(true);");
            sb.AppendLine("            YJFRxJavaUtils.mergeNoLoading(getActivity(), EnterpriseDao.enterpriseRegisterListEnterpriseRegister(");
            sb.AppendLine("                    getActivity(), String.valueOf(pageSize), String.valueOf(pageNumber),");
            sb.AppendLine("                    new StringCallback() {");
            sb.AppendLine("                        @Override");
            sb.AppendLine("                        public void onError(Call call, Exception e, int id) {");
            sb.AppendLine("                            srlRefresh.setRefreshing(false);");
            sb.AppendLine("                        }");
            sb.AppendLine("");
            sb.AppendLine("                        @Override");
            sb.AppendLine("                        public void onResponse(String response, int id) {");
            sb.AppendLine("                            srlRefresh.setRefreshing(false);");
            sb.AppendLine("                            ApiResultContainingList<Object> apis = ApiResultContainingList.fromJson(response, Object.class);");
            sb.AppendLine("                            List<Object> tmplist = apis.getData();");
            sb.AppendLine("");
            sb.AppendLine("                            if (!refreshListUtils.checkListCount(tmplist)) {");
            sb.AppendLine("                                return;");
            sb.AppendLine("                            }");
            sb.AppendLine("");
            sb.AppendLine("                            list.addAll(tmplist);");
            sb.AppendLine("                            rvlist.getAdapter().notifyDataSetChanged();");
            sb.AppendLine("                        }");
            sb.AppendLine("                    }");
            sb.AppendLine("            ));");
            sb.AppendLine("        }");
            sb.AppendLine("    }");
            sb.AppendLine("");
            sb.AppendLine("private class ListRecyclerViewAdapter extends RecyclerView.Adapter<ListViewHolder> {");
            sb.AppendLine("");
            sb.AppendLine("        @Override");
            sb.AppendLine("        public ListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {");
            sb.AppendLine("            return new ListViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout., parent, false));");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        @Override");
            sb.AppendLine("        public void onBindViewHolder(ListViewHolder holder, int position) {");
            sb.AppendLine("                     Object item=list.get(position);");
            sb.AppendLine("                    }");
            sb.AppendLine("");
            sb.AppendLine("        @Override");
            sb.AppendLine("        public int getItemCount() {");
            sb.AppendLine("            return list!=null?list.size():0;");
            sb.AppendLine("        }");
            sb.AppendLine("    }");
            sb.AppendLine("");
            sb.AppendLine("private class ListViewHolder extends RecyclerView.ViewHolder {");
            sb.AppendLine("        //单元格容器");
            sb.AppendLine("        protected LinearLayout llItem;");
            sb.AppendLine("        public ListViewHolder(View itemView) {");
            sb.AppendLine("            super(itemView);");
            sb.AppendLine("         ");
            sb.AppendLine("            llItem = itemView.findViewById(R.id.llItem);");
            sb.AppendLine("        }");
            sb.AppendLine("    }");
            sb.AppendLine("");
            return sb.ToString();

        }
    }
}
